import {warning} from './tips'
import {
  onNetworkStatusChange,
  offNetworkStatusChange,
  request,
  showModal,
  reLaunch
} from '@tarojs/taro';
// import { SET_HOME_TOKEN, SET_TOKEN } from '../store/action';
// import { getToken ,getHomeToken} from '../store/getter';

export const baseUrl="https://qo3op.com"
// export const baseUrl="https://wx.gzwrkj.cn"

export function checkLine(){
  return new Promise((resolved, rejected) => {
    onNetworkStatusChange((res) => {
      let msg = ''
      if (!res.isConnected) {
        msg = '当前网络不可用，请检查你的网络设置'
      } else if (res.networkType === 'none') {
        msg = '网络开小差了,请网络良好后重试'
      }
     msg  ?rejected(msg):resolved(res)
    })
  })
}
export function hideCheckLine(){
  offNetworkStatusChange()
} 


function Req(url, data,type){
  url =url.indexOf('http')==-1? baseUrl + url:url
  // let header;
  // let token=getToken(),homeToken=getHomeToken();
  // if (token||homeToken) {
  //   header = {}
  //  if(token) header.mtoken = token
  //  if(homeToken) header['Home-Token']=homeToken
  // }
  return new Promise((resolved, rejected) => {
    const obj = {
      url,
      data,
      // header,
      method: type,
      success: (res) => {
        res.statusCode === 200? resolved(res.data): rejected(res.data)
        // res.statusCode === 200 ? resolved(res.data) : 
      },
      fail: (err) =>{
        warning("服务器繁忙！请稍后再试",3000)
        rejected(err)
      } ,
      complete: () => {}
    }
    request(obj)
  })
}


//封装GET请求
function $get(url, data) { 
  return Req(url, data,'get')
}
//封装POST请求
function $post(url, data) { 
  return Req(url, data,'post')
}

// function $put(url, data) { 
//   return Req(url, data,'put')
// }

// function $delete(url, data) { 
//   return Req(url, data,'delete')
// }




// ------------------------智能酒店------------------------

// 未来天气
export const GET_TIAN_QI = (data) => $get("/weather/future", data)
// 实时天气
export const GET_TIAN_QI_NOW = (data) => $get("/weather/now", data)
// 生活接口
// 农历
export const GET_DAY_CALENDAR =()=>$get("/daily/calendar")

// 黄历信息
export const GET_DAY_CALENDAR_EX =()=>$get("/daily/calendar/ex")
// 黄历时辰
export const GET_DAY_CALENDAR_HOUR =()=>$get("/daily/calendar/hour")
// 每日一句{date:******}
export const GET_DAY_ONE_WORD =(data)=>$get("/daily/oneword",data)
// 生活建议
export const GET_SUGGEST =(data)=>$get("/daily/suggest",data)

// export const GET_DAY_ONE_WORD =(data)=>$get("/daily/suggest",data)























export const formatTime = (date,d=0) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()
  if(d) return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
  return [year, month, day].map(formatNumber).join('/') 
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}





